package com.xiaochen.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochen.manager.service.CategoryBrandService;
import com.xiaochen.model.dto.product.CategoryBrandDto;
import com.xiaochen.model.entity.product.Brand;
import com.xiaochen.model.entity.product.CategoryBrand;
import com.xiaochen.model.vo.common.Result;
import com.xiaochen.model.vo.common.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类-品牌管理
 * (分析):分类 和 品牌 是多对多的关系
 * eg: 手机   小米
 *     电脑   华为
 *     手表   OPPO
 *
 * @author pinkhors
 */
@Slf4j
@RestController
@RequestMapping("/admin/product/categoryBrand")
public class CategoryBrandController {

    @Autowired
    private CategoryBrandService categoryBrandService;

    /**
     * 根据分类ID查询对应的品牌数据
     */
    @GetMapping("/selectBrandByCategoryId/{categoryId}")
    public Result selectBrandByCategoryId(@PathVariable Long categoryId){
        List<Brand> list = categoryBrandService.selectBrandByCategoryId(categoryId);
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    /**
     * 多条件(CategoryBrand 分类ID 和 品牌ID)分页查询
     */
    @GetMapping("/selectByPage/{pageNum}/{pageSize}")
    public Result selectByPage(@PathVariable Integer pageNum,
                               @PathVariable Integer pageSize,
                               CategoryBrandDto categoryBrandDto){
        PageInfo<CategoryBrand> pageInfo = categoryBrandService.selectByPage(categoryBrandDto,pageNum,pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 分类-品牌的添加
     */
    @PostMapping("/saveCategoryBrand")
    public Result saveCategoryBrand(@RequestBody CategoryBrand categoryBrand){
        categoryBrandService.saveCategoryBrand(categoryBrand);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 分类-品牌的删除
     */
    @DeleteMapping("/deleteCategoryBrand/{id}")
    public Result deleteCategoryBrand(@PathVariable Long id){
        categoryBrandService.deleteCategoryBrand(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    /**
     * 分类-品牌的修改
     */
    @PutMapping("/updateCategoryBrand")
    public Result updateCategoryBrand(@RequestBody CategoryBrand categoryBrand){
        categoryBrandService.updateCategoryBrand(categoryBrand);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
